﻿package test.liu
{
	import _Pan3D.display3D.Display3DContainer;
	import _Pan3D.display3D.Display3DMovie;
	import _Pan3D.particle.DiffuseParticle;
	import _Pan3D.particle.FireParticle;
	import _Pan3D.particle.ParticleCombine;
	import _Pan3D.particle.UVParticle;
	import _Pan3D.program.Program3DManager;
	import _Pan3D.program.shaders.BaseShader;
	import _Pan3D.program.shaders.DiffuseShader;
	import _Pan3D.program.shaders.DynmicUVShader;
	import _Pan3D.program.shaders.Md5Shader;
	import _Pan3D.program.shaders.UvMoveNewShader;
	import _Pan3D.texture.TextureManager;
	
	import _me.Config;
	import _me.KeyControl;
	import _me.MathCoreWillDele;
	import _me.Scene_data;
	
	import com.adobe.utils.PerspectiveMatrix3D;
	import com.bit101.components.ColorChooser;
	import com.bit101.components.FPSMeter;
	import com.bit101.components.HSlider;
	import com.bit101.components.Label;
	import com.bit101.components.PushButton;
	import com.bit101.components.RadioButton;
	import com.bit101.components.Text;
	
	import flash.display.*;
	import flash.display3D.Context3D;
	import flash.display3D.Context3DBlendFactor;
	import flash.display3D.Context3DCompareMode;
	import flash.display3D.Context3DProgramType;
	import flash.display3D.Program3D;
	import flash.events.Event;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	import flash.utils.ByteArray;

	[SWF(backgroundColor="#FFFFFF",width="1024", height="600", frameRate="60")]
	public class CombineTest extends MovieClip {
		private var keyControl : KeyControl = new KeyControl();
		private var projMatrix : PerspectiveMatrix3D;
		private var program : Program3D;
		private var program2 : Program3D;
		
		private var diffuseParticle:DiffuseParticle;
		private var uvParticle:UVParticle;
		private var combine:ParticleCombine;


		public function CombineTest() : void {
			Scene_data.cam3D.distance = 5;
			Scene_data.focus3D.angle_x = 0;
			addEvents();
		}

		

		private function addEvents() : void {
			this.addEventListener(Event.ADDED_TO_STAGE, addToStage);
		}

		private function addToStage(event : Event) : void {
			Config.init(stage, _config_end);
			addView();
			keyControl.init(stage);
		}

		private function _config_end() : void {
			var context : Context3D = Scene_data.context3D;
			Program3DManager.getInstance().initReg();
			
			program = Program3DManager.getInstance().getProgram(DiffuseShader.DIFFUSESHADER);
			program2 = Program3DManager.getInstance().getProgram(DynmicUVShader.DYNMICUVSHADER);
			
			makeScene();
		}

		private function makeScene() : void {
			var context : Context3D = Scene_data.context3D;
			context.setDepthTest(false, Context3DCompareMode.LESS);
			context.setBlendFactors(Context3DBlendFactor.SOURCE_ALPHA,Context3DBlendFactor.ONE);
			TextureManager.getInstance().init(context);
			
			new FPSMeter(this);
			
			diffuseParticle = new DiffuseParticle(context);
			diffuseParticle.scaleChg = 2;
			diffuseParticle.allTime = 100;
			diffuseParticle.beginTime = 0;
			diffuseParticle.program = program;
			
			uvParticle = new UVParticle(context);
			uvParticle.setWHnum(3,3);
			uvParticle.beginTime = 20;
			uvParticle.speed = 3;
			uvParticle.program = program2;
			
			combine = new ParticleCombine();
			combine.addParticle(diffuseParticle);
			combine.addParticle(uvParticle);
			combine.x = 1.2;
			
			this.addEventListener(Event.ENTER_FRAME, enterFrame);
		}
		private var baseparticle:FireParticle;
		private function enterFrame(event : Event) : void {
			MathCoreWillDele._catch_cam(Scene_data.cam3D, Scene_data.focus3D);
			Scene_data.focus3D.angle_y = mouseX;

			if (!Scene_data.ready ) return;
			var context : Context3D = Scene_data.context3D;
			context.clear(bgR, bgG, bgB, 1);
			projMatrix = new PerspectiveMatrix3D();
			projMatrix.perspectiveFieldOfViewRH(1, 1, 0.1, 10000);

			context.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 0, projMatrix, true);
			//context.setProgram(program);
			
			combine.update(1);
			
			context.present();
		}
		public function addView():void{
			new Label(this,50,150,"背景颜色");
			var col:ColorChooser = new ColorChooser(stage,120,150,0xff0000,colorChose);
			col.usePopup = true;
			var r1:RadioButton = new RadioButton(this,50,95,"SOURCE_ALPHA,ONE",true,xchg);
			var r2:RadioButton = new RadioButton(this,50,110,"ONE,ONE",false,ychg);
			var r3:RadioButton = new RadioButton(this,50,125,"SOURCE_ALPHA,ONE_MINUS_SOURCE_ALPHA",false,zchg);
			new Label(this,0,580,"verson：1");
			
		}
		private function xchg(event:Event):void{
			Scene_data.context3D.setBlendFactors(Context3DBlendFactor.SOURCE_ALPHA,Context3DBlendFactor.ONE);
		}
		private function ychg(event:Event):void{
			Scene_data.context3D.setBlendFactors(Context3DBlendFactor.ONE,Context3DBlendFactor.ONE);
		}
		private function zchg(event:Event):void{
			Scene_data.context3D.setBlendFactors(Context3DBlendFactor.SOURCE_ALPHA,Context3DBlendFactor.ONE_MINUS_SOURCE_ALPHA);
		}
		private var bgR:Number = 0.5;
		private var bgG:Number = 0.5;
		private var bgB:Number = 0.5;
		private function colorChose(event:Event):void{
			var pixelValue:uint = event.target.value;
			var red:uint = pixelValue >> 16 & 0xFF;
			var green:uint = pixelValue >> 8 & 0xFF;
			var blue:uint = pixelValue & 0xFF;
			bgR = red/256;
			bgG = green/256;
			bgB = blue/256;
		}


	}
}

